package com.zbkj.service.service.mszs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.constants.MszsConstants;
import com.zbkj.common.model.mszs.MszsOrder;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.mszs.*;
import com.zbkj.common.response.mszs.MszsHotelResponse;
import com.zbkj.common.response.mszs.MszsOrderResponse;
import com.zbkj.common.response.mszs.MszsRoomOrderResponse;
import com.zbkj.common.response.mszs.MszsRoomResponse;
import com.zbkj.common.token.FrontTokenComponent;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.dao.mszs.MszsOrderDao;
import com.zbkj.service.dao.mszs.MszsRoomDao;
import com.zbkj.service.service.mszs.MszsHotelService;
import com.zbkj.service.service.mszs.MszsOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @description 民宿助手订单表
 * @author zhengkai.blog.csdn.net
 * @date 2023-11-16
 */
@Service
public class MszsOrderServiceImpl extends ServiceImpl<MszsOrderDao, MszsOrder> implements MszsOrderService  {

    @Resource
    private MszsOrderDao mszsOrderMapper;
    @Resource
    private MszsRoomDao mszsRoomDao;
    @Autowired
    private FrontTokenComponent tokenComponent;
    @Autowired
    private MszsHotelService hotelService;
    @Override
    public boolean insert(MszsOrderRequest request) {
        MszsOrder order=new MszsOrder();
        BeanUtils.copyProperties(request, order);
        int tt = mszsOrderMapper.insert(order);
        return tt > 0;
    }

    @Override
    public boolean delete(int id) {
        int ret = mszsOrderMapper.deleteById(id);
        return ret>0;
    }

    @Override
    public boolean update(MszsOrderUpdateRequest requestUpdate) {
        MszsOrder order=new MszsOrder();
        BeanUtils.copyProperties(requestUpdate, order);
        int ret = mszsOrderMapper.updateById(order);
        return ret>0;
    }

    @Override
    public boolean updateStatus(List<MszsOrder> orders) {

        boolean bool = this.updateBatchById(orders);
        return bool;
    }

    @Override
    public MszsRoomOrderResponse load(Integer id) {
        MszsRoomOrderResponse order = mszsOrderMapper.OrderDetails(id);
        MszsRoomResponse room = mszsRoomDao.roomDetails(order.getRoomId());
        if (Objects.nonNull(room)){
            order.setFjName(room.getFjName());
            String image = StringUtils.join(room.getFjImg(), ",");
            order.setFjImg(image);
            order.setFjPrice(room.getFjPrice());
            order.setFjArea(room.getFjArea());
            order.setFjWindows(room.getFjWindows());
            order.setFjFloor(room.getFjFloor());
            order.setFxName(room.getFxName());
        }

        return order;
    }


    @Override
    public List<MszsOrder> pageList(MszsOrderSearchRequest request,PageParamRequest pageParamRequest) {
        //开启分页
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        //定义查询-订单表
        LambdaQueryWrapper<MszsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件-订单号
        if (StringUtils.isNotEmpty(request.getOrderNo())){
            lambdaQueryWrapper.eq(MszsOrder::getOrderNo,request.getOrderNo());
        }
        //条件-酒店名称
        if (StringUtils.isNotEmpty(request.getJdName())){
            lambdaQueryWrapper.eq(MszsOrder::getJdName,request.getJdName());
        }
        //条件-订单状态
        if (StringUtils.isNotEmpty(request.getOrderStatus())) {
            lambdaQueryWrapper.eq(MszsOrder::getOrderStatus, request.getOrderStatus());
        }
        //条件-订单平台
        if (StringUtils.isNotEmpty(request.getGmPt())) {
            lambdaQueryWrapper.eq(MszsOrder::getGmPt,request.getGmPt());
        }
        //条件-酒店房型名称
        if (StringUtils.isNotEmpty(request.getJdfxName())) {
            lambdaQueryWrapper.eq(MszsOrder::getJdfxName,request.getJdfxName());
        }
        //排序-倒序-订单创建时间
        lambdaQueryWrapper.orderByDesc(MszsOrder::getCreateTime);

        //得到订单数据-并返回
        return mszsOrderMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<MszsOrderResponse> frontpageList(MszsOrderSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        Integer uid = tokenComponent.getUserId();
        request.setUid(uid);
        List<MszsOrderResponse> mszsOrderList = mszsOrderMapper.listOrder(request);

        return mszsOrderList;
    }

    @Override
    public Integer ordes(MszsCenterRequest request) {
        LambdaQueryWrapper<MszsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer uid = tokenComponent.getUserId();
        Integer count = 0;
        //根据登录信息 获取 酒店信息
        List<MszsHotelResponse> hotelList = hotelService.getUserHotelList(uid);
        if (request.getStatisticsType()==1){ //统计近七天
             for (MszsHotelResponse hotel:hotelList){
                Date dayDate = DateUtil.nowDateTimeReturnDate("yyyy-MM-dd"); //当前日期
                String daystr = DateUtil.addDay(dayDate,1,"yyyy-MM-dd"); //当前日期+1
                Date daystrDate = DateUtil.strToDate(daystr,"yyyy-MM-dd"); //日期字符串格式转换

                String serSeven = DateUtil.addDay(dayDate,-7,"yyyy-MM-dd"); //当前日期 -7
                Date sevenDate = DateUtil.strToDate(serSeven,"yyyy-MM-dd"); //日期字符串格式转换


                lambdaQueryWrapper.le(MszsOrder::getCreateTime,daystrDate);
                lambdaQueryWrapper.ge(MszsOrder::getCreateTime,sevenDate);
                lambdaQueryWrapper.eq(MszsOrder::getJdId,hotel.getId());
                Integer cou = mszsOrderMapper.selectCount(lambdaQueryWrapper);
                count = count+ cou;
            }
        }

        return count;
    }

    @Override
    public Integer checkIns(MszsCenterRequest request) {
        LambdaQueryWrapper<MszsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        Integer uid = tokenComponent.getUserId();
        Integer count = 0;
        //根据登录信息 获取 酒店信息
        List<MszsHotelResponse> hotelList = hotelService.getUserHotelList(uid);
        if (request.getStatisticsType()==1){ //统计近七天
            for (MszsHotelResponse hotel:hotelList){
                Date dayDate = DateUtil.nowDateTimeReturnDate("yyyy-MM-dd"); //当前日期
                String daystr = DateUtil.addDay(dayDate,1,"yyyy-MM-dd"); //当前日期+1
                Date daystrDate = DateUtil.strToDate(daystr,"yyyy-MM-dd"); //日期字符串格式转换

                String serSeven = DateUtil.addDay(dayDate,-7,"yyyy-MM-dd"); //当前日期 -7
                Date sevenDate = DateUtil.strToDate(serSeven,"yyyy-MM-dd"); //日期字符串格式转换

                lambdaQueryWrapper.le(MszsOrder::getCreateTime,daystrDate);
                lambdaQueryWrapper.ge(MszsOrder::getCreateTime,sevenDate);
                lambdaQueryWrapper.eq(MszsOrder::getJdId,hotel.getId());
                lambdaQueryWrapper.eq(MszsOrder::getOrderStatusNumber,2);
                Integer cou = mszsOrderMapper.selectCount(lambdaQueryWrapper);
                count = count+ cou;
            }
        }

        return count;
    }

    @Override
    public List<MszsOrder> ordersTask() {
        LambdaQueryWrapper<MszsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MszsOrder::getOrderStatusNumber,2);
        List<MszsOrder> orders = mszsOrderMapper.selectList(lambdaQueryWrapper);

        return orders;
    }

    @Override
    public List<MszsRoomOrderResponse> roomOrders(MszsRoomSelectRequest request) {
        PageHelper.startPage(request.getPage(), request.getLimit());
        List<MszsRoomOrderResponse> orders = mszsOrderMapper.selecRoomOrders(request);
        return orders;
    }

    @Override
    public MszsRoomOrderResponse roomCheckIn(int roomid) {
        MszsRoomOrderResponse response = mszsOrderMapper.selectOrder(roomid);
        return response;
    }

    @Override
    public MszsOrder getWherePhoneOne(String userPhone) {
        LambdaQueryWrapper<MszsOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MszsOrder::getUserPhone,userPhone);
        lambdaQueryWrapper.eq(MszsOrder::getIsDel,false);
        lambdaQueryWrapper.eq(MszsOrder::getIsShow,true);
        return mszsOrderMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public MszsOrder getWhereOrder6One(String order6) {
        // 构建查询条件
        QueryWrapper<MszsOrder> wrapper = new QueryWrapper<>();
        wrapper.like("order_no", "%" + order6 + "%");
        wrapper.eq("order_status_number", MszsConstants.MSZS_ORDER_STATUS_INT_check_in);//已入住
        //wrapper.eq("is_del",0);
        //wrapper.eq("is_show",1);
        wrapper.orderByDesc("create_time"); // 可选，根据需要排序
        return this.getOne(wrapper,false);
    }

    @Override
    public List<MszsOrder> taskOrderList() {
        // 构建查询条件
        LambdaQueryWrapper<MszsOrder> wrapper = new LambdaQueryWrapper<>();

        wrapper.orderByDesc(MszsOrder::getCreateTime); // 可选，根据需要排序
        return mszsOrderMapper.selectList(wrapper);
    }

}
